package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mp.domain.User;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("用户接口管理")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;
    @ApiOperation("新增用户")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO)
    {
        //转换为User
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }
    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable("id") Long id)
    {
        userService.removeById(id);
    }
    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public UserVO queryById(@PathVariable("id") Long id)
    {
        User byId = userService.getById(id);
        UserVO userVO = BeanUtil.copyProperties(byId, UserVO.class);
        return userVO;
    }
    @ApiOperation("根据id批量查询用户")
    @GetMapping
    public List<UserVO> queryByIds(@PathVariable("ids")List<Long> ids)
    {
        List<User> users = userService.listByIds(ids);
        return BeanUtil.copyToList(users, UserVO.class);
    }

    /**
     * 根据ID更新用户余额
     * @param id 用户id
     * @param amount 扣减的金额
     */
    @ApiOperation("根据id更新余额")
    @PutMapping("/{id}/deduction/{amount}")
    public void updateBalanceById(@PathVariable("id")Long id,@PathVariable("amount")int amount)
    {
        userService.deductionBalanceById(id, amount);
    }
    @ApiOperation("根据条件查询用户列表")
    @PostMapping("/list")
    public List<UserVO> queryList(@RequestBody UserQuery userQuery)
    {
        String username=userQuery.getName();
        Integer status=userQuery.getStatus();
        Integer minBalance=userQuery.getMinBalance();
        Integer maxBalance=userQuery.getMaxBalance();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        /*queryWrapper.lambda()
                //成立条件：字段：查询的关键字；如果第一个值为true才会设置当前这个条件到where中
                .like(StrUtil.isNotBlank(username),User::getUsername,username)
                .eq(status!=null,User::getStatus,status)
                .ge(minBalance!=null,User::getBalance,minBalance)
                .le(maxBalance!=null,User::getBalance,maxBalance);*/
        List<User> userList = userService.lambdaQuery()
                .like(StrUtil.isNotBlank(username), User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();//最终进行查询
        return BeanUtil.copyToList(userList, UserVO.class);
    }
}
